#include "connectioninfo.h"
#include "ui_connectioninfo.h"
#include "deviceconnectdao.h"
#include "interfacedao.h"
#include "in_performance.h"
#include "devicedao.h"
#include "device.h"
#include <QTableWidgetItem>
ConnectionInfo::ConnectionInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConnectionInfo)
{
    ui->setupUi(this);
}

ConnectionInfo::~ConnectionInfo()
{
    //    int count = ui->conn_detail->rowCount();
    //    if(count>0){
    //        for(int i=count-1;i>=0;i--){
    //            QTableWidgetItem *item = ui->conn_detail->item(i,0);
    //            delete item;
    //        }
    //        ui->conn_detail->clearContents();
    //        ui->conn_detail->clear();
    //    }
    delete ui;
}
void ConnectionInfo::initOther(int leftdeviceid, QString desc1, int rightdeviceid, QString desc2)
{
    DeviceDao dd;
    InterfaceDao ido;
    Device *left =  dd.getDeviceById(leftdeviceid);
    Device *right = dd.getDeviceById(rightdeviceid);
    int total = 1;
    ui->conn_detail->setRowCount(total);
    int i = 0;
    if (left == NULL)
    {
        left  = new Device;
    }
    if (right == NULL)
    {
        right  = new Device;
    }
    {

        Device_interface di;
        di = ido.getByDeviceIdAndDesc(leftdeviceid, desc1);
        QString name = left->getName();
        if (name == "")
        {
            name = left->getName();
        }
        QTableWidgetItem *item = new QTableWidgetItem(name);
        ui->conn_detail->setItem(i, 0, item) ;
        QTableWidgetItem *item0 = new QTableWidgetItem(di.getDescription());
        ui->conn_detail->setItem(i, 1, item0);
        int speed = di.getSpeed();
        if (speed > 0)
        {
            speed = speed / 1000000;
        }
        QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(speed) + "M");
        ui->conn_detail->setItem(i, 2, item1);
        In_performance *lt = new In_performance ;
        //        if(left->getOlt()==1||left->getDeviceType()==9||left->getDeviceType()==6){
        //            lt = ip.getPerformanceByParameter(conn->getLeftDeviceId(),conn->leftInterface,"Utilization",1);
        //        }
        //        else{
        //            lt = ip.getPerformanceByParameter(conn->getLeftDeviceId(),conn->leftInterface,"Utilization",2);
        //        }
        if (lt->get_pid() != -1)
        {
            QTableWidgetItem *item2 = new QTableWidgetItem("");
            ui->conn_detail->setItem(i, 3, item2);
        }
        else
        {
            QTableWidgetItem *item2 = new QTableWidgetItem("");
            ui->conn_detail->setItem(i, 3, item2);
        }
        delete lt;
    }
    {
        Device_interface di1;
        di1 = ido.getByDeviceIdAndDesc(rightdeviceid, desc2);
        QString name = right->getName();
        if (name == "")
        {
            name = right->getName();
        }
        QTableWidgetItem *item = new QTableWidgetItem(name);
        ui->conn_detail->setItem(i, 4, item) ;
        QTableWidgetItem *item0 = new QTableWidgetItem(di1.getDescription());
        ui->conn_detail->setItem(i, 5, item0);
        QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(di1.getSpeed() / 1000000) + "M");
        ui->conn_detail->setItem(i, 6, item1);
        In_performance *lt  = new In_performance ;
        //        if(right->getOlt()==1||right->getDeviceType()==9||right->getDeviceType()==6){
        //            lt = ip.getPerformanceByParameter(conn->getRightDeviceId(),conn->rightInterface,"Utilization",1);
        //        }
        //        else{
        //            lt = ip.getPerformanceByParameter(conn->getRightDeviceId(),conn->rightInterface,"Utilization",2);
        //        }
        if (lt->get_pid() != -1)
        {
            QTableWidgetItem *item2 = new QTableWidgetItem("");
            ui->conn_detail->setItem(i, 7, item2);
        }
        else
        {
            QTableWidgetItem *item2 = new QTableWidgetItem("");
            ui->conn_detail->setItem(i, 7, item2);
        }
        delete lt;
    }
}

void ConnectionInfo::init(int leftdeviceid, int rightdeviceid)
{
    DeviceConnectDao cd;
    list<Deviceconnect *> ds = cd.getConnBy2DeviceId(leftdeviceid, rightdeviceid);
    DeviceDao dd;
    Device *left =  dd.getDeviceById(leftdeviceid);
    Device *right = dd.getDeviceById(rightdeviceid);
    if (left == NULL)
    {
        left  = new Device;
    }
    if (right == NULL)
    {
        right  = new Device;
    }
    InterfaceDao ido;
    In_performance ip;
    int total = ds.size();
    ui->conn_detail->setRowCount(total);
    int i = 0;
    foreach(Deviceconnect * conn, ds)
    {
        {

            Device_interface di;
            if (left->getOlt() == 1)
            {
                di  = ido.getInterfaceDetail(conn->leftInterface, conn->getLeftDeviceId());
            }
            else
            {
                di  = ido.getInterfaceDetailByIndex(conn->leftInterface, conn->getLeftDeviceId());
            }

            QString name = left->getName();
            if (name == "")
            {
                name = left->getName();
            }
            QTableWidgetItem *item = new QTableWidgetItem(name);
            ui->conn_detail->setItem(i, 0, item) ;
            QTableWidgetItem *item0 = new QTableWidgetItem(di.getDescription());
            ui->conn_detail->setItem(i, 1, item0);
            int speed = di.getSpeed();
            if (speed > 0)
            {
                speed = speed / 1000000;
            }
            QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(speed) + "M");
            ui->conn_detail->setItem(i, 2, item1);
            In_performance *lt ;
            if (left->getOlt() == 1 || left->getDeviceType() == 9 || left->getDeviceType() == 6)
            {
                lt = ip.getPerformanceByParameter(conn->getLeftDeviceId(), conn->leftInterface, "Utilization", 1);
            }
            else
            {
                lt = ip.getPerformanceByParameter(conn->getLeftDeviceId(), conn->leftInterface, "Utilization", 2);
            }
            if (lt->get_pid() != -1)
            {
                QTableWidgetItem *item2 = new QTableWidgetItem(QString::number(lt->get_value()));
                ui->conn_detail->setItem(i, 3, item2);
            }
            else
            {
                QTableWidgetItem *item2 = new QTableWidgetItem("");
                ui->conn_detail->setItem(i, 3, item2);
            }
            delete lt;
        }
        {
            Device_interface di1;
            if (right->getOlt() == 1)
            {
                di1 = ido.getInterfaceDetail(conn->rightInterface, conn->getRightDeviceId());
            }
            else
            {
                di1 = ido.getInterfaceDetailByIndex(conn->rightInterface, conn->getRightDeviceId());
            }
            QString name = right->getName();
            if (name == "")
            {
                name = right->getName();
            }
            QTableWidgetItem *item = new QTableWidgetItem(name);
            ui->conn_detail->setItem(i, 4, item) ;
            QTableWidgetItem *item0 = new QTableWidgetItem(di1.getDescription());
            ui->conn_detail->setItem(i, 5, item0);
            QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(di1.getSpeed() / 1000000) + "M");
            ui->conn_detail->setItem(i, 6, item1);
            In_performance *lt ;
            if (right->getOlt() == 1 || right->getDeviceType() == 9 || right->getDeviceType() == 6)
            {
                lt = ip.getPerformanceByParameter(conn->getRightDeviceId(), conn->rightInterface, "Utilization", 1);
            }
            else
            {
                lt = ip.getPerformanceByParameter(conn->getRightDeviceId(), conn->rightInterface, "Utilization", 2);
            }
            if (lt->get_pid() != -1)
            {
                QTableWidgetItem *item2 = new QTableWidgetItem(QString::number(lt->get_value()));
                ui->conn_detail->setItem(i, 7, item2);
            }
            else
            {
                QTableWidgetItem *item2 = new QTableWidgetItem("");
                ui->conn_detail->setItem(i, 7, item2);
            }
            delete lt;
        }
        delete conn;
        i++;
    }
    delete left;
    delete right;
    ds.clear();
}

void ConnectionInfo::on_conn_detail_doubleClicked(const QModelIndex &index)
{

}
